/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.app.*
import com.almasb.fxgl.core.math.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.dsl.components.*
import com.almasb.fxgl.entity.*
import javafx.beans.property.*
import javafx.geometry.*
import javafx.scene.paint.*
import javafx.util.*

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
class BmpSample : GameApplication() {
	private enum class Type {
		PLAYER, CRYSTAL
	}
	
	override fun initSettings(settings: GameSettings) {
		settings.width = 1066
		settings.title = "Crystal Chase"
	}
	
	override fun initGameVars(vars: MutableMap<String, Any>) {
		vars["entities"] = 0
		vars["tpf"] = 0.0
	}
	
	override fun initGame() {
		getGameWorld().addEntityFactory(MyFactory())
		getGameScene().setBackgroundColor(Color.BLACK)
		
		val text = getUIFactory().newText("", Color.WHITE, 24.0)
		text.textProperty().bind(getip("entities").asString("Entities: %d"))
		
		addUINode(text, 25.0, 25.0)
		
		val text2 = getUIFactory().newText("", Color.WHITE, 24.0)
		text2.textProperty().bind(SimpleDoubleProperty(1.0).divide(getdp("tpf")).asString("FPS: %.2f"))
		
		addUINode(text2, 25.0, 50.0)
		
		run({ this.spawnCrystal() }, Duration.seconds(3.0))
	}
	
	private fun spawnCrystal() {
		val numToSpawn = 100
		
		for(i in 0 until numToSpawn) {
			spawn("crystal")
		}
		
		inc("entities", +numToSpawn)
	}
	
	override fun onUpdate(tpf: Double) {
		set("tpf", tpf)
	}
	
	class MyFactory : EntityFactory {
		
		@Preload(100)
		@Spawns("crystal")
		fun newEntity(data: SpawnData): Entity {
			return entityBuilder().at(FXGLMath.randomPoint(Rectangle2D(0.0, 0.0, (getAppWidth() - 55).toDouble(), (getAppHeight() - 55).toDouble())))
				.type(Type.CRYSTAL)
				.viewWithBBox(texture("ball.png", 32.0, 32.0))
				.with(RandomMoveComponent(Rectangle2D(0.0, 0.0, getAppWidth().toDouble(), getAppHeight().toDouble()), 250.0))
				//.view(texture("YellowCrystal.png").toAnimatedTexture(8, Duration.seconds(0.66)).loop())
				.build()
		}
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(BmpSample::class.java, args)
}
